const mixin = require('../../mixins');
Component({
  behaviors: [mixin],
  data: {
    currSrc: "",
    currTimeObj: {}, // 当前倒计时
    children: [],
    visible: false, // 是否显示倒计时
    dayShow: false,
    hourShow: false,
    minuteShow: false,
    secondShow: false,
    isShowCustom: 0,
    customText: null,
    customStyle: null,
    timerPhase: 0,  //  未开始：0、进行中：1、已结束：2
  },
  properties: {
    mf_data: Object,
  },
  async attached() {
    //  是否展示自定义
    await this.showCustomCountDown(this.properties.mf_data);
    this.initChildren();
    this.initData();
  },
  detached() {
    this.timer && clearInterval(this.timer);
  },
  methods: {
    // 初始化倒计时渲染的数据
    initChildren() {
      const that = this;
      const { mf_data } = this.properties;
      let children = [];
      mf_data.children.map((item) => {
        let id = item.id
        if(!item.disabled){
          if (id.includes('day')) {
            item.id = 'day';
            that.data.dayShow = true;
          } else if (id.includes('hour')) {
            item.id = 'hour';
            that.data.hourShow = true;
          } else if (id.includes('minute')) {
            item.id = 'minute';
            that.data.minuteShow = true;
          } else if (id.includes('second')) {
            item.id = 'second';
            that.data.secondShow = true;
          }
          // return item;
          children.push(item);
        }
      })
      this.setData({
        children
      })
    },
    //  是否展示自定义倒计时格式
    async showCustomCountDown(mf_data){
      try{
        const regex = /^{.*}$/;
        if(mf_data && mf_data.label1 && regex.test(mf_data.label1)){
          const _obj = JSON.parse(mf_data.label1);
          if( _obj && _obj.isCustom){
            this.setData({
              isShowCustom: 2,
              customText: _obj.text || "",
              customStyle: _obj.style || "",
            })
            return true;
          }
        }
      }catch(e){
      }
      this.setData({
        isShowCustom: 1
      })
      return true;
    },
    // 初始化数据
    async initData() {
      const { mf_data } = this.properties;
      const { startDate, date: endData, startSrc, src, finishSrc } = mf_data;
      //  获取服务器时间
      // const now = await getApp().globalData.getServerTime();
      const now = new Date().getTime();
      const isStart = now - new Date(startDate).getTime();
      const isEnd = new Date(endData).getTime() - now;

      if (isStart < 0) { // 判断是否倒计时开始
        this.toStartCountDown();
        this.setData({
          visible: false,
          currSrc: startSrc,
          timerPhase: 0
        })
      } else if (isEnd < 0) {  // 判断是否倒计时结束
        this.setData({
          visible: false,
          currSrc: finishSrc,
          timerPhase: 2
        })
      } else {  // 倒计时正在进行
        this.setData({
          currSrc: src,
          visible: true,
          timerPhase: 1
        })
        this.countDown();
      }
    },
    // 倒计时未开始 启动定时器 用于倒计时开始时切换
    toStartCountDown() {
      const { startDate } = this.properties.mf_data;
      this.timer = setInterval(async () => {
        //  获取服务器时间
        // const now = await getApp().globalData.getServerTime();
        const now = new Date().getTime();
        const isStart = now - new Date(startDate).getTime();
        if (isStart >= 0) {
          clearInterval(this.timer);
          this.initData();
        }
      }, 1000);
    },

    // 倒计时进行中
    countDown() {
      this.timer = setInterval(async () => {
        const { date: endData } = this.properties.mf_data;
        //  获取服务器时间
        // const now = await getApp().globalData.getServerTime();
        const now = new Date().getTime();
        const timeStamp = new Date(endData).getTime() - now;
        if (timeStamp <= 0) { // 倒计时结束 重新触发初始化fn 走结束逻辑
          clearInterval(this.timer);
          this.initData();
          return
        }
        this.setData({
          currTimeObj: this.getDate(timeStamp)
        })
      }, 1000);
    },
    // 获取两个事件的差值 时间戳
    timeGap(start, end) {
      return new Date(end).getTime() - new Date(start).getTime()
    },
    // 根据当前时间戳处理数据 划分时分秒
    getDate(timeStamp) {
      const { mf_data }= this.properties;
      const {dayShow, hourShow, minuteShow, secondShow} = this.data;
      let day = Math.floor(timeStamp / (1000 * 60 * 60 * 24));
      let hour = Math.floor(timeStamp / (1000 * 60 * 60) % 24);
      let minute = Math.floor(timeStamp / (1000 * 60) % 60);
      let second = Math.floor(timeStamp / 1000 % 60);
      let _obj = {};
      if(dayShow && hourShow && minuteShow && secondShow){
        _obj.day = mf_data.textMode ? this.checkTime(day) : this.checkTime(day).split('');
        _obj.hour = mf_data.textMode ? this.checkTime(hour) : this.checkTime(hour).split('');
        _obj.minute = mf_data.textMode ? this.checkTime(minute) : this.checkTime(minute).split('');
        _obj.second = mf_data.textMode ? this.checkTime(second) : this.checkTime(second).split('');
      }
      if(!dayShow && hourShow && minuteShow && secondShow){
        hour = day * 24 + hour;
        _obj.hour = mf_data.textMode ? this.checkTime(hour) : this.checkTime(hour).split('');
        _obj.minute = mf_data.textMode ? this.checkTime(minute) : this.checkTime(minute).split('');
        _obj.second = mf_data.textMode ? this.checkTime(second) : this.checkTime(second).split('');
      }
      if(!dayShow && !hourShow && minuteShow && secondShow){
        minute = (day * 24 + hour) * 60 + minute;
        _obj.minute = mf_data.textMode ? this.checkTime(minute) : this.checkTime(minute).split('');
        _obj.second = mf_data.textMode ? this.checkTime(second) : this.checkTime(second).split('');
      }
      if(!dayShow && !hourShow && !minuteShow && secondShow){
        second = ((day * 24 + hour) * 60 + minute) * 60 + second;
        _obj.second = mf_data.textMode ? this.checkTime(second) : this.checkTime(second).split('');
      }
      return _obj;
    },
    checkTime(num) {
      if (Number(num) < 10) {
        return "0" + num
      }
      return String(num);
    },
  },
});
